import * as moment from 'moment' ;
import dTalkApi from '../core/dTalkApi';
import emplModel from '../model/corpEmplModel';
import tsModel from '../model/bpmTsModel';
import BPM from '../lib/db/BPM';
const schedule = require("node-schedule");
class SyncDataService{
    constructor(){}
   
    //定时任务管理方法
    async run2(){
      
        let j1 = await schedule.scheduleJob('00 00 07 * * *', function () {
            console.log('上午____同步钉钉人员数据开始!!!!!!!!')
            let res_=syncData.getEmplFromDD().catch(error=>{
                console.log('getEmplFromDD_Error',error.message,'error.message');
            });
            console.log('上午____同步钉钉人员数据结束!!!!!!!!')
        });
    
        let j2 = await schedule.scheduleJob('00 10 20 * * *', function () {
            console.log('晚上____同步钉钉人员数据开始!!!!!!!!')
            let res_=syncData.getEmplFromDD().catch(error=>{
                console.log('getEmplFromDD_Error',error.message,'error.message');
            });
            console.log('晚上___同步钉钉人员数据结束!!!!!!!!')
        });

        /*  await syncData.getEmplFromDD().catch(error=>{
            console.log('getEmplFromDD_Error',error.message,'error.message');
        }) */

    }
  
    //同步钉钉获取人员信息
    async getEmplFromDD(){
        console.log('获取部门人员开始!!!!!!!!!!')
        let data=await dTalkApi.getSondepIdList('1',true);
        let deptIdList:any=data.department;
        if(deptIdList.length>0){
            for (let index = 0; index < deptIdList.length; index++) {
                let id = deptIdList[index];
                let empdata=await dTalkApi.getDeptEmplDetail(id);
               // console.log(empdata,'<------------>')
                if(empdata&&empdata.userlist){
                    let empList:any[]=empdata.userlist
                    if(empList.length>0){
                        for (let empindex = 0; empindex < empList.length; empindex++) {
                            let emp = empList[empindex];
                            let data1=await emplModel.findOne({where:{userid:emp.userid}})
                            if(data1){
                                let a_= await emplModel.update({
                                    userid: emp.userid,
                                    username: emp.name,
                                    name:emp.name,
                                    telephone:emp.mobile,//手机号码
                                    jobnumber:emp.jobnumber,   //用户工号
                                    comment:1
                                }as any,({where:{userid:emp.userid}}))
                                //console.log(a_,'a_a_a_a_a_',emp.name)
                            }else{
                                let b=await emplModel.create({
                                   
                                    userid: emp.userid,
                                    username: emp.name,
                                    name: emp.name,
                                    telephone:emp.mobile,//手机号码
                                    jobnumber:emp.jobnumber,   //用户工号
                                    comment:1
                                }as any)
                                console.log(b.getDataValue,'<-----------empindex----',empindex)
                            }
                        }
                    }
                }
            }
        }
        console.log('获取钉钉人员结束!!!!!!!!!!')
       return {code:0}
    }

    /**
     * http://211.95.6.53:8080/esbpm/LoginHandler.ashx?GUID=ce70f22a9ead41eaace4f2eefce38776&url=wx/wxflow.aspx?type=1    复兴
     * 
     * 
     * 
     * 查询未读信息处理人的工号,发送未读流程
    */
    async unRead(){
        console.log('1111111111111111111')
        let count = await tsModel.count();
        if (count == 0) {
            console.log('not set ts')
            return;
        }
        let tsFind = await tsModel.findOne({ where: { tId: "1" } });
        if (!tsFind) {
            return;
        }
        let data = await BPM.query({
            query: `select LoginName,ApplyName,Name,Subject,StartTime,MessageId,GUID,ts FROM View_Flow_Undo_dd where ts>? `, 
            values: [new Date(tsFind.ts.getTime() - 1000 * 60 * 60 * 8)]
        })
        if(data){
            for (let index = 0; index < data[0].length; index++) {
                let element = data[0][index];
                let ApplyName = element.ApplyName;
                let MessageId = element.MessageId;
                let jobnumber = element.LoginName;
                let GUID = element.GUID;
                let Name = element.Name;
                let userid = element.userid;
                let StartTime = element.StartTime;
                let empMp = await emplModel.findOne({ where: { jobnumber: jobnumber } });
                let duserid:any=''
                if(empMp&&empMp.userid){
                    duserid = empMp.userid;
                }
                //发送消息
                let msg = await dTalkApi.sendMsg(
                    {
                        "touser": `${duserid}`,
                        "agentid": "205473493",
                        "msgtype": "oa",
                        "oa": {
                            "message_url": `http://211.95.6.53:8080/esbpm/LoginHandler.ashx?GUID=${GUID}&url=wx/wxFlowDetail.aspx?MessageId=${MessageId}`,
                            "head": {
                                "bgcolor": "FFBBBBBB",
                                "text": "柏豪BPM"
                            },
                            "body": {
                                "title": "待办任务",
                                "form": [
                                    {
                                        "key": "发起人:",
                                        "value": `${ApplyName || '-'}`
                                    },
                                    {
                                        "key": "标题:",
                                        "value": `${Name}`
                                    }
                                ]
                            }
                        }
                    }
    
                );
                await tsFind.update({ ts: element.ts });
            }
        }
    }




    
}
let syncData=new SyncDataService();
export default syncData;
